import React from 'react'
import { Form, Input, Button, DatePicker, Select } from 'antd'
import util from '@/utils'
import moment from 'moment'
import './search.css'

const FormItem = Form.Item

const { Option } = Select

const BatchRecordSearch = (props) => {
  const { 
    batchId, 
    content,
    status,
    auditBatchStatusEnum,
    startTime, 
    endTime, 
    onChangeBatchId,
    onChangeContent,
    onChangeStatus,
    onChangeStartTime,
    onChangeEndTime,
    onSearch,
  } = props

  return (
    <Form layout="inline" style={{ padding: 24 }}>
      <FormItem label="批次编号" colon={false}>
        <Input style={{ width: 120 }} placeholder="请输入批次编号" value={batchId} onChange={onChangeBatchId} />
      </FormItem>
      <FormItem label="短信内容" colon={false}>
        <Input style={{ width: 120 }} placeholder="请输入短信内容" value={content} onChange={onChangeContent} />
      </FormItem>
      <FormItem label="审核状态" colon={false}>
        <Select style={{ width: 120 }} value={status} onChange={onChangeStatus}>
          {auditBatchStatusEnum.map(({ index, desc }) => <Option key={index} value={index}>{desc}</Option>)}
        </Select>
      </FormItem>
      <FormItem label="开始时间" colon={false}>
        <DatePicker style={{ width: 120 }} disabledDate={disabledStartDateWeek.bind(null, endTime)} defaultValue={startTime} onChange={onChangeStartTime} allowClear={false} />
      </FormItem>
      <FormItem label="结束时间" colon={false}>
        <DatePicker style={{ width: 120 }} disabledDate={disabledEndDateWeek.bind(null, startTime)} defaultValue={endTime} onChange={onChangeEndTime} />
      </FormItem>
      <FormItem>
        <Button type="primary" onClick={onSearch}>查询</Button>
      </FormItem>
    </Form>
  )
}

// 只能选择从今天往前一个星期的时间
export const disabledDate = (date) => {
  if(!date) return
  let now = new Date()
  now.setDate(now.getDate() + 1)
  let tomorrow = util.getStartTime(moment(now))
  now.setDate(now.getDate() - 7)
  const frontWeek = util.getStartTime(moment(now));
  return (date.valueOf() > tomorrow.valueOf()) || (date.valueOf() < frontWeek.valueOf())
}

// 开始时间不能大于结束时间
export const disabledStartDate = (endDate, date) => {
  if(!endDate || !date) return
  return (date.valueOf() > endDate.valueOf()) 
}

// 结束时间不能小于开始时间
export const disabledEndDate = (startDate, date) => {
  if(!startDate || !date) return
  return (date.valueOf() < startDate.valueOf())
}

// 只能选择从今天往前一个星期的时间 并且 开始时间不能大于结束时间
export const disabledStartDateWeek = (endDate, date) => {
  if(!endDate) return
  return disabledDate(date) || disabledStartDate(endDate, date)
}

// 只能选择从今天往前一个星期的时间 并且 结束时间不能小于开始时间
export const disabledEndDateWeek = (startDate, date) => {
  if(!startDate) return
  return disabledDate(date) || disabledEndDate(startDate, date)
}

export default Form.create()(BatchRecordSearch)